package cn.tradewin.advisor.pages.workflow;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;
import org.tynamo.security.services.SecurityService;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.commons.util.CodeTableSymbols;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.model.Vacation;
import cn.tradewin.advisor.persist.services.CodetableService;
import cn.tradewin.advisor.persist.services.VacationService;

public class AddVacation {
	
	@Property
	private Vacation vacation;
	
	@Property
	private SelectModel categoryModel;
	
	@Inject
	private Logger log;
	
	@Inject
	private VacationService vacationService;
	
	@Inject
	private CodetableService codetableService;
	
	@Inject
	private SecurityService securityService;
	
	@Inject
	private ProcessEngine engine;
	
	@Inject
	private TaskService taskService;
	
	@Inject
	private IdentityService identityService;
	
	@Inject
    private RuntimeService runtimeService;
	
	@Property
	private String dateFormat="yyyy-MM-dd";
	
	@Log
	void onPrepareForRender() throws Exception {
		Subject subject = securityService.getSubject();
		String accountId = (String)subject.getPrincipal();
		
		if (vacation == null) {
			vacation = new Vacation();
		}
		vacation.setId(Long.valueOf(vacationService.getMaxId(accountId)));
		List<CodeLabel> categories = codetableService.listCategory(CodeTableSymbols.VACATION_TYPE, "zh");
		categoryModel = new CodeLabelSelectModel(categories);
	}
	
	@OnEvent(EventConstants.PREPARE)
	void onPrepare() throws Exception {
		if (vacation == null) {
			vacation = new Vacation();
		}
	}

	@Log
	Object onSubmit() {
		try {
			Subject subject = securityService.getSubject();
			String accountId = (String)subject.getPrincipal();
			vacation.setAccountId(accountId);
			vacation.setStatus(0);
			vacation.setApplyDate(new Date());
			//启动流程，并把流程实例的ID存入业务数据库中。
		    vacation.setProcessId(startProcess(vacation));
			vacationService.addVaction(vacation);		
			List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("manager").list();
			log.info("=================================================");
			for (Task task : tasks) {
				log.info(task.getProcessVariables().get("employeeName")+"");
				log.info(task.getName());
			}
			log.info("=================================================");
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		return VacationBoard.class;
	}
	
	private String startProcess(Vacation info) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("employeeName", info.getAccountId());
		variables.put("numberOfDays", new Integer(4));
		variables.put("vacationMotivation", "I'm really tired!");
		variables.put("vacation", info);
		identityService.setAuthenticatedUserId("fangyn");
		return runtimeService.startProcessInstanceByKey("vacationRequest", variables).getProcessInstanceId();
	}
	
	void test() {
		RepositoryService service = null;
	}
}